package com.suzus.eight;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.suzus.utils.RabbitMqUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Consumer01 {

    public static final String NORMAL_EXCHANGE="normal_exchange";
    public static final String DEAD_EXCHANGE="dead_exchange";
    public static final String NORMAL_QUEUE="normal_queue";
    public static final String DEAD_QUEUE="dead_queue";


    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();

        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DEAD_EXCHANGE,BuiltinExchangeType.DIRECT);

        Map<String, Object>arguments=new HashMap<>();
        arguments.put("x-message-ttl",100000);
        arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        arguments.put("x-dead-letter-routing-key","lisi");
        //arguments.put("x-max-length",6);

        channel.queueDeclare(NORMAL_QUEUE,false,false,false,arguments);

        channel.queueDeclare(DEAD_QUEUE,false,false,false,null);

        channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"zhangsan");
        channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");

        System.out.println("waiting information");

        DeliverCallback deliverCallback = (var1,var2)->{
            String s = new String(var2.getBody(), StandardCharsets.UTF_8);
            if(s.equals("info5")){
                System.out.println("Refuse");
            }
            else {
                System.out.println("C1: " + new String(var2.getBody(), StandardCharsets.UTF_8));
                channel.basicReject(var2.getEnvelope().getDeliveryTag(),false);
            }
        };

        channel.basicConsume(NORMAL_QUEUE,false, deliverCallback,consumerTag->{});

    }
}
